package org.toolbook.validator;

import org.toolbook.model.User;
import org.toolbook.util.MD5Util;
import org.toolbook.util.PropUtil;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;
/**
 * 修改密码的验证器
 * @author meigang
 * @date 2015-06-21 22:09
 *
 */
public class UpdatePwdValidator extends Validator{

	@Override
	protected void validate(Controller c) {
		//验证原密码
		User u = c.getSessionAttr(PropUtil.getProperty("SESSION_LOGIN_KEY"));
		if(!MD5Util.md5(c.getPara("oldPwd")).equals(u.get("password"))){
			addError("oldPwd_info", "当前密码输入错误");
		}
		validateString("newPwd", 8, 16, "newPwd_info", "新密码长度为【8-16】");
		if(c.getPara("oldPwd").equals(c.getPara("newPwd"))){
			addError("newPwd_info", "新密码不能与原密码一致");
		}
		validateEqualField("newPwd", "rePwd", "rePwd_info", "两次密码输入不一致");
	}

	@Override
	protected void handleError(Controller c) {
		c.keepPara("oldPwd");
		c.keepPara("newPwd");
		c.setAttr("nopass", 1);
		c.render("/WEB-INF/pages/fc/user/updatePwd.jsp");
	}

}
